package com.zhongmai.yzfgrabanorder.utils;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.zhongmai.yzfgrabanorder.base.MyApplication;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static android.content.Context.MODE_PRIVATE;

/**
 * 项目名称：YZFGrabAnOrder
 * 类描述：
 * 创建人：Administrator
 * 创建时间：2020/9/23 13:07
 * 修改人：Administrator
 * 修改时间：2020/9/23 13:07
 * 修改备注：
 */
public class Utils {
    public static String getValueFromSP(Context context, String name, String key) {
        LogUtils.debug("getValueFromSP name = " + name + " key " + key, 6);
        String result = getSharedPreferences(context, name).getString(key, "");
        return result;
    }

    private static SharedPreferences getSharedPreferences(Context context, String name) {
        SharedPreferences preferences = context.getSharedPreferences(name, MODE_PRIVATE);
        LogUtils.debug("getValueFromSP name = " + name + " size " + preferences.getAll().size(), 6);
        return preferences;
    }

    public static boolean save2SP(Context context, String name, String key, String value) {
        LogUtils.debug("save2SP name = " + name + " key " + key + " value " + value, 6);
        SharedPreferences preferences = getSharedPreferences(context, name);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, value);
        return edit.commit();
    }

    public static boolean delFromSP(Context context, String name, String key) {
        SharedPreferences preferences = getSharedPreferences(context, name);
        SharedPreferences.Editor edit = preferences.edit();
        return edit.remove(key).commit();
    }

    /**
     * 格式化时间
     *
     * @return
     */
    public static String formatTime(long longTime) {
        Date date = new Date(longTime);
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sd.format(date);
    }

    public static void setClipboard(Context context, String text) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("copy_str", text);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
    }

    private void delayTime() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
            }
        }, 5000);    //延时5s执行
    }

    /**
     * 域名获取IP
     *
     * @param host
     * @return
     */
    public static void parseHostGetIPAddress(final String host) {
        final String[][] ipAddressArr = {null};
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InetAddress[] inetAddressArr = InetAddress.getAllByName(host);
                    if (inetAddressArr != null && inetAddressArr.length > 0) {
                        ipAddressArr[0] = new String[inetAddressArr.length];
                        for (int i = 0; i < inetAddressArr.length; i++) {
                            ipAddressArr[0][i] = inetAddressArr[i].getHostAddress();
                        }
                    }
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                for (String s : ipAddressArr[0]) {
                    LogUtils.bugErr("ip------: " + s);
                }
                if (!TextUtils.isEmpty(ipAddressArr[0][0])) {
                    Constants.ip = ipAddressArr[0][0];
                }
            }
        }).start();

    }


    /**
     * 获取设备唯一标识
     */
    public static String getPhoneMark() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(Utils.getSerialNumber());
        stringBuffer.append("_"+android.os.Build.SERIAL);
        stringBuffer.append("_"+android.os.Build.BRAND);
        stringBuffer.append("_"+android.os.Build.MODEL);
        String s = stringBuffer.toString();
        return s;
    }

    /**
     * 获取设备ID
     * @return
     */
    public static String getSerialNumber() {
        String SerialNumber = null;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            SerialNumber = Build.SERIAL;
        } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
            SerialNumber = Settings.System.getString(MyApplication.getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        } else {
            if (ActivityCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

                return SerialNumber;
            }
            SerialNumber = Build.getSerial();
        }
        return SerialNumber;
    }
}
